home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SERVICES.PAK / MEMORY.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  260 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Services Library
  3. // Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.20  $
  6. //
  7. // Reliable platform independent header for common memory and string functions
  8. //
  9. //----------------------------------------------------------------------------
  10. #if !defined(SERVICES_MEMORY_H)
  11. #define SERVICES_MEMORY_H
  12.  
  13. #if !defined(SERVICES_DEFS_H)
  14. # include <services/defs.h>
  15. #endif
  16. #if defined(BI_COMP_BORLANDC)
  17. # include <mem.h>
  18. #else
  19. # include <memory.h>
  20. #endif
  21. #if !defined(__STRING_H) && !defined(_INC_STRING)
  22. # include <string.h>
  23. #endif
  24. #if !defined(__TCHAR_H)
  25. # include <tchar.h>
  26. #endif
  27.  
  28. #if !defined(SERVICES_WSYSINC_H)
  29. # include <services/wsysinc.h>
  30. #endif
  31.  
  32. #if defined(BI_PLAT_MSW)
  33. //
  34. // To support double-byte and extended characters, map the standard string
  35. // functions to the windows calls (but not if they are macros)
  36. //
  37. # if defined(_Windows)
  38. inline int _RTLENTRYF _tcscmp(const _TCHAR far* s1, const _TCHAR far* s2)
  39.                    {return lstrcmp(s1, s2);}
  40. inline int _RTLENTRYF _tcsicmp(const _TCHAR far* s1, const _TCHAR far* s2)
  41.                    {return lstrcmpi(s1, s2);}
  42. #   if !defined(strcmpi)
  43. inline int       strcmpi(const _TCHAR far* s1, const _TCHAR far* s2)
  44.                    {return lstrcmpi(s1, s2);}
  45. #   endif //strcmpi
  46. # else    //This is DOS
  47. inline int _RTLENTRYF _tcscmp(const _TCHAR far* s1, const _TCHAR far* s2)
  48.                    {return strcmp(s1, s2);}
  49. inline int _RTLENTRYF _tcsicmp(const _TCHAR far* s1, const _TCHAR far* s2)
  50.                    {return strcmpi(s1, s2);}
  51. #   if !defined(strcmpi)
  52. inline int       strcmpi(const _TCHAR far* s1, const _TCHAR far* s2)
  53.                    {return strcmpi(s1, s2);}
  54. #   endif
  55. # endif //_Windows
  56. #endif //BI_PLAT_MSW
  57.  
  58. #if defined(BI_DBCS_SUPPORT)
  59.   inline uint      CharSize(const _TCHAR far* s) {return ::AnsiNext(s) - s;}
  60. #else
  61.   inline uint      CharSize(const _TCHAR far*) {return sizeof(_TCHAR);}
  62. #endif
  63.  
  64. #if defined(BI_PLAT_MSW)
  65. inline _TCHAR CharUpper(_TCHAR c) {::AnsiUpperBuff(&c,1); return c;}
  66. inline _TCHAR CharLower(_TCHAR c) {::AnsiLowerBuff(&c,1); return c;}
  67. #endif
  68.  
  69. #if defined(BI_NAMESPACE)
  70. namespace ClassLib {
  71. #endif
  72.  
  73. //
  74. //
  75. //
  76. template<class T> class TCharIterator {
  77.   public:
  78.     TCharIterator(T far* p);
  79.  
  80.     T far* Next() const;
  81.     T far* Current() const;
  82.  
  83.     T far* operator ++();    // prefix
  84.     T far* operator ++(int); // postfix
  85.  
  86.     operator T far*() const;
  87.  
  88.   protected:
  89.     T far* P;
  90. };
  91.  
  92. //
  93. //
  94. //
  95. template<class T> class TBidirCharIterator : public TCharIterator<T> {
  96.   public:
  97.     TBidirCharIterator(T far* begin, T far* p);
  98.  
  99.     T far* Prev() const;
  100.  
  101.     T far* operator --();    // prefix
  102.     T far* operator --(int); // postfix
  103.  
  104.   private:
  105.     T far* Begin;
  106. };
  107.  
  108. //
  109. template<class T> inline TCharIterator<T>::TCharIterator(T far* p)
  110. :
  111.   P(p)
  112. {
  113. }
  114.  
  115. //
  116. template<class T> inline T far* TCharIterator<T>::Next() const
  117. {
  118. #if defined(BI_DBCS_SUPPORT)
  119.   return *P ? ::AnsiNext(P) : P + 1;
  120. #else
  121.   return P + 1;
  122. #endif
  123. }
  124.  
  125. //
  126. template<class T> inline T far* TCharIterator<T>::Current() const
  127. {
  128.   return P;
  129. }
  130.  
  131. //
  132. template<class T> inline T far* TCharIterator<T>::operator ++()
  133. {
  134.   return P = Next();
  135. }
  136.  
  137. //
  138. template<class T> inline T far* TCharIterator<T>::operator ++(int)
  139. {
  140.   T far* p = P;
  141.   P = Next();
  142.   return p;
  143. }
  144.  
  145. //
  146. template<class T> inline TCharIterator<T>::operator T far*() const
  147. {
  148.   return P;
  149. }
  150.  
  151. //
  152. template<class T> inline TBidirCharIterator<T>::TBidirCharIterator(T far* begin, T far* p)
  153. :
  154.   P(p), Begin(begin)
  155. {
  156. }
  157.  
  158. //
  159. template<class T> inline T far* TBidirCharIterator<T>::Prev() const
  160. {
  161. #if defined(BI_DBCS_SUPPORT)
  162.   return ::AnsiPrev(Begin, P);
  163. #else
  164.   return P > Begin ? P - 1 : P;
  165. #endif
  166. }
  167.  
  168. //
  169. template<class T> inline T far* TBidirCharIterator<T>::operator --()    // prefix
  170. {
  171.   return P = Prev();
  172. }
  173.  
  174. //
  175. template<class T> inline T far* TBidirCharIterator<T>::operator --(int) // postfix
  176. {
  177.   T far* p = P;
  178.   P = Prev();
  179.   return p;
  180. }
  181.  
  182. #if defined(BI_NAMESPACE)
  183. } // namespace ClassLib
  184. #endif
  185.  
  186. //
  187. // Inline versions of memory.h & string.h functions, overloaded for far ptrs
  188. // for use when in a near data model.
  189. //
  190. #if defined(BI_DATA_NEAR)
  191.   inline void far* memccpy(void far* d, const void far* s, int c, size_t n)
  192.                      {return _fmemccpy(d, s, c, n);}
  193.   inline void far* memchr(const void far* s, int c, size_t n)
  194.                      {return _fmemchr(s, c, n);}
  195.   inline int       memcmp(const void far* s1, const void far* s2, size_t n)
  196.                      {return _fmemcmp(s1, s2, n);}
  197.   inline void far* memcpy(void far* d, const void far* s, size_t n)
  198.                      {return _fmemcpy(d, s, n);}
  199.   inline int       memicmp(const void far* s1, const void far* s2, size_t n)
  200.                      {return _fmemicmp(s1, s2, n);}
  201.   inline void far* memset(void far* s, int c, size_t n)
  202.                      {return _fmemset(s, c, n);}
  203.   inline void far* memmove(void far* d, const void far* s, size_t n)
  204.                      {return _fmemmove(d, s, n);}
  205.  
  206.   inline _TCHAR far* _tcscat(_TCHAR far* d, const _TCHAR far* s)
  207.                      {return _fstrcat(d, s);}
  208.   inline _TCHAR far* _tcschr(const _TCHAR far* s, int c) {return _fstrchr(s, c);}
  209.   inline _TCHAR far* _tcscpy(_TCHAR far* d, const _TCHAR far* s)
  210.                      {return _fstrcpy(d, s);}
  211.   inline size_t    _tcslen(const _TCHAR far* s) {return _fstrlen(s);}
  212.   inline _TCHAR far* _tcslwr(_TCHAR far* s) {return _fstrlwr(s);}
  213.   inline _TCHAR far* strncat(_TCHAR far* d, const _TCHAR far* s, size_t maxlen)
  214.                      {return _fstrncat(d, s, maxlen);}
  215.   inline int       _tcsncmp(const _TCHAR far* s1, const _TCHAR far* s2, size_t maxlen)
  216.                      {return _fstrncmp(s1, s2, maxlen);}
  217.   inline _TCHAR far* _tcsncpy(_TCHAR far* d, const _TCHAR far* s, size_t maxlen)
  218.                      {return _fstrncpy(d, s, maxlen);}
  219.   inline int       _tcsnicmp(const _TCHAR far* s1, const _TCHAR far* s2, size_t maxlen)
  220.                      {return _fstrnicmp(s1, s2, maxlen);}
  221.   inline _TCHAR far* _tcsrchr(const _TCHAR far* s, int c) {return _fstrrchr(s, c);}
  222.   inline _TCHAR far* _tcstok(_TCHAR far* s1, const _TCHAR far* s2)
  223.                      {return _fstrtok(s1, s2);}
  224.   inline _TCHAR far* _tcsupr(_TCHAR far* s) {return _fstrupr(s);}
  225. #endif
  226.  
  227. //
  228. // Type overloaded version of Window's huge mem copy (hmemcpy) for flat memory
  229. // models.
  230. //
  231. #if defined(BI_PLAT_WIN32)
  232.   inline void      hmemcpy(void* d, const void* s, long n) {memcpy(d, s, n);}
  233. #endif
  234.  
  235. //
  236. // A C++ version of strdup(), strnewdup(). Uses new char[], to allow duplicated
  237. // strings to be deleted[].
  238. // Also a far string version of atol for near data models
  239. //
  240. _TCHAR* _SVCSFUNC strnewdup(const _TCHAR* s, size_t minAllocSize = 0);
  241. #if defined(BI_DATA_NEAR)
  242.   _TCHAR far* _SVCSFUNC strnewdup(const _TCHAR far* s, size_t minAllocSize = 0);
  243.   _TCHAR* _SVCSFUNC nstrnewdup(const _TCHAR far* s, size_t minAllocSize = 0);
  244.   long atol(const _TCHAR far* s);
  245. #else
  246. # define nstrnewdup strnewdup
  247. #endif
  248.  
  249. #if !defined(BI_PLAT_WIN16)
  250. # if !defined(_WCHAR_T_DEFINED)
  251.     typedef unsigned short wchar_t;
  252. #   define _WCHAR_T_DEFINED
  253. # endif
  254.   wchar_t* _SVCSFUNC strnewdup(const wchar_t* s, size_t minAllocSize = 0);
  255.   wchar_t* _SVCSFUNC _tcscpy(wchar_t* dst, const wchar_t* src);
  256.   size_t   _SVCSFUNC _tcslen(const wchar_t* s);
  257. #endif
  258.  
  259. #endif  // SERVICES_MEM_H
  260.